/*
 * st7735_spi_stm32.c
 *
 *  Created on: Dec 14, 2012
 *      Author: Andrey Belomutskiy
 */

#include <stm32f4xx_rcc.h>
#include <stm32f4xx_spi.h>
#include "st7735_spi_stm32.h"


void initSpiForLcd() {
	SPI_InitTypeDef SPI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Enable SPIx and GPIOx clocks */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

	/**
	 * GPIO pins
	 */
	GPIO_InitStructure.GPIO_Pin = PAL_PORT_BIT(GPIO_Pin_CS)
			| PAL_PORT_BIT(GPIO_Pin_DC) | PAL_PORT_BIT(GPIO_Pin_RST);
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIO_LCD, &GPIO_InitStructure);

	/**
	 * Alternative Function pins - SCK and MOSI
	 */

	GPIO_InitStructure.GPIO_Pin = PAL_PORT_BIT(GPIO_Pin_SCK)
			| PAL_PORT_BIT(GPIO_Pin_MOSI);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_Init(GPIO_LCD, &GPIO_InitStructure);

	GPIO_PinAFConfig(GPIO_LCD, GPIO_Pin_SCK, GPIO_AF_SPI1 );
	GPIO_PinAFConfig(GPIO_LCD, GPIO_Pin_MOSI, GPIO_AF_SPI1 );

	/*
	 * SPI1 configuration
	 * 72MHz / 4 = 18MHz
	 */
//	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
//	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
//	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
//	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
//	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
//	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
//
//	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
//	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//	SPI_InitStructure.SPI_CRCPolynomial = 7;


	  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
	  SPI_InitStructure.SPI_CRCPolynomial = 7;



	SPI_Init(SPI1, &SPI_InitStructure);
	/* Enable SPI1  */
	SPI_Cmd(SPI1, ENABLE);
}

/**
 * @brief  Sends a byte through the SPI interface and return the byte
 *   received from the SPI bus.
 * @param byte : byte to send.
 * @retval : The value of the received byte.
 */
u8 SPI_LCD_SendByte(u8 byte) {
	SPI_I2S_ReceiveData(SPI1 );
	/* Loop while DR register in not emplty */
	while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE ) == RESET)
		;
	/* Send byte through the SPI1 peripheral */
	SPI_I2S_SendData(SPI1, byte);
	/* Wait to receive a byte */
	while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE ) == RESET)
		;
	/* Return the byte read from the SPI bus */
	return SPI_I2S_ReceiveData(SPI1 );
}

void spiLcdCmd(int c) {
//	SPI_LCD_CS_LOW();         // Select the device
	SPI_LCD_DC_LOW();         // sending command
	(void) SPI_LCD_SendByte(c);
//	SPI_LCD_CS_HIGH();         // Deselect the device
}

void spiLcdData(int c) {
//	SPI_LCD_CS_LOW();         // Select the device
	SPI_LCD_DC_HIGH();        // sending data
	(void) SPI_LCD_SendByte(c);
//	SPI_LCD_CS_HIGH();        // Deselect the device
}

